/********************************************************************************
Title: Data Preparation for Colombia Sample
		Define time points
		Define variables to use in each index
		Define survey round to use as reference round
********************************************************************************/

loc ref round == 0
*----------------------------------

use "${raw}/colombia/colombia_desc", clear
keep if !mi(round)
sort pid date
collapse (firstnm) age ses female, by(pid)
tempfile desc 
save `desc'


use "${raw}/colombia/colombia_desc", clear
keep pid date_dmy round
duplicates drop
keep if !mi(round)
isid pid round
tempfile date
save `date'

use  "${raw}/colombia/colombia.dta", clear
merge m:1 pid using `desc', nogen
merge 1:1 pid round using `date', nogen
drop date
ren date_dmy date
*replace date = date_dmy if !mi(date_dmy)





*-------------------------------------------------------------------------------
*			0. Formating and Renaming Variables
*-------------------------------------------------------------------------------

bysort pid (date): gen ref = _n == 1 & date < mdy(3,15,2020)
loc ref ref == 1

*			0.0 Heterogeneity Variables
*-------------------------------------------------------------------------------
egen highses = cut(ses), group(2)


*			0.1		PID and HHID
*-------------------------------------------------------------------------------
gen hhid = pid
encode pid, gen(pid1)
drop pid
ren pid1 pid


*			0.2		Outcomes
*-------------------------------------------------------------------------------
global scl "scl_5 scl_14 scl_15 scl_20 scl_22 scl_26 scl_29 scl_30 scl_31 scl_32 scl_54 scl_71 scl_79"
foreach var of varlist $scl { 
	clonevar `var'_old = `var' // keeping original names of scl variables
}
ren (scl_2 scl_5 	scl_6		scl_11 	scl_13 	scl_14 	scl_15 	scl_17 	scl_19 	scl_20 	scl_21 	scl_22) ///
	(nerv  losssex	critical	annoyed	afraid	energy	endlife	tremble	aptte	crying	shy		trapped)
ren	(scl_23 	scl_24 	scl_25 	scl_26 	scl_29 	scl_30 	scl_31 	scl_32 	scl_33 	scl_34 	scl_36 	scl_37 ) ///
	(scared		temper	afraidH	blame	lonely	blue	worry	intrst	fear	feelhrt	unsymp	unfriendly)
ren	(scl_39 	scl_41 	scl_47 	scl_50 	scl_54 	scl_57 	scl_61 	scl_63 	scl_67 		scl_69 	scl_71 	scl_72 ) ///
	(heart		infrr	afrtrvl	avoid	hoplss	tense	uneasy	hrmothr	brkobjct	slfcncs	effort	terror )
ren	(scl_73 	scl_74 	scl_75 	scl_78 	scl_79 		scl_80 		scl_81 	scl_82 	scl_86 ) ///
	(uncmfteat	argmnt	nervaln	restlss	worthlss	badthngs	shout	faint	disheart)
codebook nerv-disheart

ren *_old * //Original names of scl variables

*			0.3		Month
*-------------------------------------------------------------------------------
*gen d = dofm(date)
*drop date 
*ren d date
gen month = month(date) + 10 + 12*year(date) - 12*2019
tab month


*-------------------------------------------------------------------------------
*			1. Generating Variable Lists
*-------------------------------------------------------------------------------
macro drop depression* 
gl depression energy endlife aptte crying blame blue intrst infrr hoplss effort worthlss

foreach g in  depression {
	foreach var of varlist $`g' {
		count if mi(`var')
		if `r(N)' == 145 gl `g'_nm $`g'_nm `var'
	}
}

*-------------------------------------------------------------------------------
*			2. Generating Indices
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
*			2. Generating SCL-90 score
*-------------------------------------------------------------------------------
global scl "scl_5 scl_14 scl_15 scl_20 scl_22 scl_26 scl_29 scl_30 scl_31 scl_32 scl_54 scl_71 scl_79"

egen nmissing = rowmiss(`scl') 
tab nmissing // n=0

egen scl_score = rsum($scl)  if scl_5<. //  higher score shows worse MH
replace scl_score= scl_score/13 // divide by # of questions
lab var scl_score  "Depression score"

gen ___depression_nw = scl_score


* Checking Cronbach's alpha
*Validated tool SCL-90
alpha $scl
 // Scale reliability coefficient= 0.855
*Weighted depression score (in the commented code)
alpha energy endlife aptte crying blame blue intrst infrr hoplss effort worthlss  // Scale reliability coefficient= 0.823



foreach var in  $depression {
	sum `var' if `ref'
	replace `var' = (`var' - `r(mean)')/`r(sd)'
	gen swt_`var' = `var'
}


factor $depression_nm  if  `ref', ipf factor(1)
predict ___depression_fw



*Inverse-covariance-weighted index
icw_index($depression_nm), gen(___depression_icw)

foreach var of varlist ___* {
	replace `var' = -1*`var'
}

reg ___depression_nw highses

// Scoring of the scale is made by giving a score between 0 and 4 for the choices on each item (0=None, 1=Little, 2=Medium level, 3=Much, 4=Too much). The score of the individual regarding the subscale is found by adding the scores given to the items of that subscale dividing into the number of items in the subscale. Therefore, the score of the individual regarding each subscale is found. Increase of the score indicates the increase of psychological symptom level. Scores from 0.00 up to 1.50 is Normal Symptom, scores from 1.51 up to 2.50 is High Level Symptom, scores from 2.51 up to 4.00 is Very High Level



